import React, { useState } from 'react';
import { Form, Field ,Dialog} from 'react-vant'
import { withRouter } from 'react-router';
import request from '../../../utils/request';
 function Reg2(props) {
    let { setFlag } = props
    const [account, setaccount] = useState() //帐号
    const [pass, setpass] = useState() //密码
    return (
        <div>
            <div className='Regtitle'>
                微信号/QQ号/邮箱号登录
            </div>
            <div className='inpbox'>
                <Form>
                    <Form.Item
                        name="text1"
                        label="帐号"
                    >
                        <Field placeholder="请填写微信号/QQ号/邮箱登录" onChange={(e) => {
                            setaccount(e)
                        }} />
                    </Form.Item>
                    <Form.Item
                        name="text2"
                        label="密码"
                    // rules={[
                    //     {
                    //         validator: (_, value) => {
                    //             if (/^(?=.*[a-zA-Z])(?=.*[0-9])[A-Za-z0-9]{8,18}$/.test(value)) {
                    //                 return Promise.resolve(true);
                    //             }
                    //             return Promise.reject(new Error('请输入正确的密码，密码必须由字母、数字组成，密码长度为8-18位'));
                    //         },
                    //     },
                    // ]}
                    >
                        <Field type='password' placeholder="请输入密码" onChange={(e) => {
                            setpass(e)
                        }} />
                    </Form.Item>
                </Form>
                <p onClick={() => {
                    setFlag(0)
                }}>
                    <span>用手机号登录</span>
                </p>
            </div>
            <div className='Btn_box'>
                <button onClick={() => {
                    if(account && pass !== ''){
                        request.post('/my/Otherways', { account, pass }).then((res) => {
                            if (res.data.code === 0) {
                                localStorage.setItem('token', res.data.Userid)
                                props.history.push('/main')
                            } else if(res.data.code === 1){
                                Dialog.alert({
                                    message: res.data.message,
                                }).then(() => {
                                    // on close
                                });
                            }else{
                                Dialog.alert({
                                    message: res.data.message,
                                }).then(() => {
                                    // on close
                                });
                            }
                        })
                    }else{
                        Dialog.alert({
                            message: '请填写帐号或密码',
                        }).then(() => {
                            // on close
                        });
                    }          
                }}>登录</button>
            </div>
        </div>
    );
}
export default withRouter(Reg2)

